package com.zyx.ip2region.utils;

import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/31 16:55
 */
public class IpUtil {

    public static String getCityInfo(String ip, int algorithm) {
        // db文件下载地址: https://gitee.com/lionsoul/ip2region/tree/master/data
        String dbPath = IpUtil.class.getResource("/db/ip2region.db").getPath();
        File file = new File(dbPath);
        if (!file.exists()) {
            System.out.println("Error: Invalid ip2region.db file...");
        }
        // 查询算法 B-tree
        // int algorithm = DbSearcher.BTREE_ALGORITHM;
        DbSearcher searcher = null;
        try {
            DbConfig config = new DbConfig();
            searcher = new DbSearcher(config, dbPath);
            Method method = null;
            switch (algorithm) {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
                default:
                    break;
            }
            if (!AuxUtil.isIpAddress(ip)) {
                System.out.println("Error: Invalid ip address");
                return null;
            }
            DataBlock dataBlock;
            if (method != null) {
                dataBlock = (DataBlock) method.invoke(searcher, ip);
                return dataBlock.getRegion();
            } else {
                System.out.println("Error: Invalid DbSearcher method...");
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (searcher != null) {
                    searcher.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getCityInfoWithBytes(String ip, byte[] dbBinStr) {

        String region = null;
        DbSearcher searcher = null;

        try {

            DbConfig config = new DbConfig();
            searcher = new DbSearcher(config, dbBinStr);

            if (AuxUtil.isIpAddress(ip)) {
                System.out.println("Error: Invalid ip address");
            }

            DataBlock dataBlock = searcher.memorySearch(ip);
            region = dataBlock.getRegion();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return region;
    }



    public static void main(String[] args) {
        String cityIpString = getCityInfo("39.104.58.151", DbSearcher.BTREE_ALGORITHM);
        System.out.println(cityIpString);
        /*String[] splitIpString = cityIpString.split("\\|");
        String province = splitIpString[2].replaceAll("\\省", "");
        String city = splitIpString[3].replaceAll("\\市", "");
        System.out.println(province);
        System.out.println(city);*/
    }

}
